/// **************************************************************************************************
/// "Copyright (c) 2007 Sai Panyam, Inc.  All rights reserved."
/// **************************************************************************************************
/// Component Name   :	Attribute, Member of System72.NTextile 
/// Description      :	Attribute description
/// Author           :	Sai Panyam
/// Creation Date    :	November 09 2007
/// Modified Date    :              
/// Modified By      :
/// **************************************************************************************************
using System;

namespace System72.NTextile
{
    public class Attribute
    {
        #region Private Variables

        private string m_attributeKey;
        private string m_attributeValue;
        private string m_operatorOpen;
        private string m_operatorClose;
        private readonly AttributeType m_attributeType;

        #endregion

        #region Constructor
        /// <summary>
        /// 
        /// </summary>
        /// <param name="attributeType"></param>
        public Attribute(AttributeType attributeType)
        {
            m_attributeType = attributeType;
            initializeByAttributeType();
        }
        
        #endregion
        
        #region Properties
        /// <summary>
        /// 
        /// </summary>
        public string AttributeValue
        {
            get
            {
                return m_attributeValue;
            }
            set
            {
                m_attributeValue = value;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public string OperatorOpen
        {
            get
            {
                return m_operatorOpen;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public string OperatorClose
        {
            get
            {
                return m_operatorClose;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public string AttributeKey
        {
            get
            {
                return m_attributeKey;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public AttributeType AttributeType
        {
            get { return m_attributeType; }
        }

        #endregion

        #region Public Methods
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string GetAttributeKeyValueText()
        {
            string attributeText = String.Empty;
            if (attributeText != String.Empty || m_attributeValue != String.Empty)
            {
                //We need special processing of splitting classid into class and id
                if(m_attributeValue.Contains("#") && m_attributeValue.IndexOf("#")!=0 && m_attributeType==AttributeType.ClassID)
                {
                    int indexOfHash = m_attributeValue.IndexOf("#");
                    int attributeValueLength = m_attributeValue.Length;
                    //Class value will be from the begining of the string index:0 till the symbol '#'
                    //which will be string length minus the index of # incremented by one.
                    //In plain english, from the begining till the character one before than # 
                    string classValue = m_attributeValue.Substring(0,attributeValueLength-(indexOfHash+1));
                    //id value will be from the character one after # till the end of string.
                    string idValue = m_attributeValue.Substring(indexOfHash+1); ;
                    string idAttributeKey = "id";
                    attributeText =String.Format("{0}{1}{2}{3}{4}{0}{5}{2}{6}{4}{0}",Util.Space,m_attributeKey,Util.EqualsDoubleQuote,classValue,Util.DoubleQuote,idAttributeKey,idValue); 
                }
                else
                {
                    attributeText = String.Format("{0}{1}{2}{3}{4}{0}", Util.Space, m_attributeKey, Util.EqualsDoubleQuote, m_attributeValue, Util.DoubleQuote);
                }
                
            }
            return attributeText;
        }

        #endregion

        #region Private Methods
        /// <summary>
        /// 
        /// </summary>
        private void initializeByAttributeType()
        {
            switch (m_attributeType)
            {
                case AttributeType.Class:
                    m_attributeKey = "class";
                    m_operatorOpen = "(";
                    m_operatorClose = ")";
                    break;
                case AttributeType.ClassID://This is same as Class as we need to process class first and then the ID attribute
                    m_attributeKey = "class";
                    m_operatorOpen = "(";
                    m_operatorClose = ")";
                    break;
                case AttributeType.ID:
                    m_attributeKey = "id";
                    m_operatorOpen = "(#";
                    m_operatorClose = ")";
                    break;
                case AttributeType.Language:
                    m_attributeKey = "lang";
                    m_operatorOpen = "[";
                    m_operatorClose = "]";
                    break;
                case AttributeType.Style:
                    m_attributeKey = "style";
                    m_operatorOpen = "{";
                    m_operatorClose = "}";
                    break;
                default:
                    throw new NotSupportedException("AttributeType :" + m_attributeType + " not yet supported");
            }
        }

        #endregion

    }
}
